<?php
/**
 * 国誉
 *
 * @author lwb
 */
namespace app\wechat\controller;

class GuoyuFund extends Common {

    protected $config;
    protected $params;
    protected $env = APP_ENV;

    public function __construct() {
        parent::__construct();
        $this->config = config('guoyu.' . $this->env);
    }
    
    //鉴权
    public function auth($params){
        //用户信息
        $userinfo = $this->getWS('ucenter', 'users@userInfo', ['u_code' => $this->uid]);
        
        $this->params = [
            'service' => 'GYJF0005',//接口服务代码
            'u_code' => $this->uid,//用户uid
            'CUSTOMERNAME' => $userinfo['result']['u_true_name'],//客户姓名
            'CERIFICATETYPE' => '01',//证件类型
            'IDCARDNO' => $userinfo['result']['u_ident_no'],//证件号码
            'TELEPHONENUMBER' => $userinfo['result']['u_name'],//手机号码
            'CARDNO' => $userinfo['result']['ub_card_no'],//银行卡号
        ];
        
        $return = $this->returnMsg();
        
        if($return['result']['RES_CODE'] != '800000'){
            $result = ['code' => 5000, 'msg' => $return['result']['RES_MSG']?:'鉴权失败'];
            return $this->parse_data($result);
        }
        
        session('requestId', $return['result']['requestId']);
        //是否发送短信
        $smscode_flag = false;
        if($return['result']['message2'] == 'Y' || $return['result']['businessTypeId'] == '1' || $return['result']['businessTypeId'] == '2'){
            $smscode_flag = true;
        }
        session('smscode_flag', $smscode_flag);
        
        $result = ['data' => ['smscode_flag' => $smscode_flag]];
        
        return $this->parse_data($result);
    }

    //开通账户
    public function openAccount($params) {
        //获取参数
        $code = !empty($params['smscode'])?$params['smscode']:'123456';
        //资方配置
        $fund_config = config('fund.' . $this->env);
        
        if(session('smscode_flag') && empty($code)){
            $result = ['code' => 5000, 'msg' => '短信验证码必填'];
            return $this->parse_data($result);
        }
        
        $this->params = [
            'service' => 'GYJF0007',//接口服务代码
            'u_code' => $this->uid,//用户uid
            'REQUESTID' => session('requestId'),//GYJF0005返回
            'SMSCODE' => $code,
            'ORDER_FLAG' => '1',
        ];
        
        $return = $this->returnMsg();
        
        if(empty($return['result']['Pwdurl'])){
            $result = ['code' => 5000, 'msg' => $return['result']['RES_MSG']?:'接口错误'];
            return $this->parse_data($result);
        }
        
        //保存电子账户
        $return = $this->getWS('ucenter', 'fund@saveAccount', ['u_code' => $this->uid, 'fundName' => 'guoyu', 'accountno' => $return['result']['message1']]);
        
        if($return['status'] != '2000'){
            $result = ['code' => 5000, 'msg' => '保存电子账户失败'];
            return $this->parse_data($result);
        }
        
        //设置密码连接 处理跳转地址
        list($path, $params) = explode('?',$result['Pwdurl']);
        $paramsArr = explode('&', $params);
        $paramsArr[6] = 'redirect=' . $fund_config['back_url'];	
        $paramStr = implode('&', $paramsArr);			 
        $url = $path . '?' . $paramStr;
        
        $result = ['data' => ['url' => $url]];
        
        return $this->parse_data($result);
    }
   
    //协议
    public function agreement(){
        $file_type = request()->param('type');
        //请求的类型转换
        $typeArr = array(
            '01' => 'C0', //静默代扣协议
            '02' => 'C1', //电子账户协议
            '03' => 'D1', //个人借款额度合同
            '04' => 'E0', //央行查询授权协议
            '05' => 'J0', //借款(贷款)要素确认单
            '06' => 'H0', //受托支付委托书 （签名过的）
        );
        $this->params = [
            'service' => 'GYJF0707',//接口服务代码
            'u_code' => $this->uid,//用户uid
            'FILE_TYPE' => $typeArr[$file_type],
            'LOAN_NO_' => '',
            'FLAG' => '2',
        ];
        
        $return = $this->returnMsg();
        
        return file_get_contents($return['result']['URL']);
    }
    
    //调用
    protected function returnMsg(){
        $return  = $this->getWS('channel', 'guoyufund@guoyuInterface', $this->params);
        
        return $return;
    }
    
}
